package org.snail.attendence.service.impl;

import lombok.extern.log4j.Log4j;
import org.snail.attendence.bean.AliWorkDTO;
import org.snail.attendence.service.AttendenceMongoService;
import org.snail.employee.bean.EmployeesDO;
import org.snail.employee.service.impl.EmployeesServiceImpl;
import org.snail.exception.CommonException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Log4j
@Service
public class AttendenceMongoOptionService {
    @Autowired
    private AttendenceMongoService attendenceMongoService;
    @Autowired
    private EmployeesServiceImpl employeesService;

    public void saveAttendence(Object att,String mongoName) throws CommonException {
        attendenceMongoService.saveAttendenceMongo(att,mongoName);
    }

    public void saveAttendenceList(List<AliWorkDTO> aliWorkDTOs, String mongoName) throws CommonException {
        //保存mongo的list
        List<AliWorkDTO> mongoSaveList = new ArrayList<>();
        for(AliWorkDTO aliWorkDTO : aliWorkDTOs){
            if (aliWorkDTO.getState() == 1) {
                EmployeesDO empByAliOrName = employeesService.findEmpByNumOrName(aliWorkDTO.getAliNumber(),aliWorkDTO.getWorkNum(),aliWorkDTO.getEmployeeName());
                if(empByAliOrName!=null){
                    mongoSaveList.add(aliWorkDTO);
                }else{
                    log.error("mongoDB保存考勤信息时发现此员工在人员表中未找到,可能人员未同步");
                }
            }
        }
        //保存数据至mongoDB
        attendenceMongoService.saveAttendenceList(aliWorkDTOs,mongoName);
    }

}